package org.vaillant.todo

import grails.test.*

class TaskIntegrationTests extends GroovyTestCase {
	
	def taskService
	
	def statusHistoryService
	
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testCreateTask() {

		User user = new User(userId:'david')
		
		assertNotNull user.save()
		
		user.addToTasks(new Task(description:'First task!'))
		user.addToTasks(new Task(description:'Second task!'))
		
		def foundTasks = User.get(user.id).tasks
		
		assertEquals 2,foundTasks.size()
		
		
    }
	
	void testDeleteTask() {
		
		User user = new User(userId:'david')
		
		assertNotNull user.save()
		
		user.addToTasks(new Task(description:'First task!'))
		user.addToTasks(new Task(description:'Second task!'))
		
		def foundTasks = User.findById(user.id).tasks.collect { it }
		
		println foundTasks
		
		println foundTasks[0].description
		
		assertTrue Task.exists(foundTasks[0].id)
		
		user.delete()
		
		assertFalse Task.exists(foundTasks[0].id)
		
	}
	
	void testAddStatus() {
		
		User user = new User(userId:'david')
		
		assertNotNull user.save()
		
		Status openStatus = new Status(name:'open')
		Status closeStatus = new Status(name:'close')
		
		Task t1 = new Task(description:'First task!')
		assertNotNull t1.save()
		
		StatusHistory st1 = new StatusHistory()
		t1.addToStatus(st1)
		openStatus.addToStatusHistory(st1)
		
		StatusHistory st2 = new StatusHistory()
		t1.addToStatus(st2)
		
		closeStatus.addToStatusHistory(st2)
		user.addToTasks(t1)
		
		def foundStatus = Task.get(t1.id).status*.status
		
		foundStatus.each { println it }
		assertEquals(['close','open'],foundStatus.name)
		
	}
	
	void testAddComment(){
		
		Task t1 = new Task(description:'First task!')
		assertNotNull t1.save()
		
		t1.addToComments(new Comment(message:'first comment'))
		t1.addToComments(new Comment(message:'second comment'))
		
		def foundTasks = Task.get(t1.id)
		
		assertEquals 2,foundTasks.comments.size()
		
		assertEquals(['first comment','second comment'],foundTasks.comments*.message)
		
	}
	
	
}
